'===============================================================================
' IndiaStockQuotes Component Sample Application.
' Author : Hemant Kamalakar
' Email  : hemant.kamalakar@gmail.com
'===============================================================================

Imports System
Imports System.Text
Imports System.Collections.Generic
Imports System.Drawing
Imports DotnetIndia.StockQuotes.NSE
Imports DotnetIndia.StockQuotes

Public Class frmStockTest
    ' Reset inputs
    Private Sub ResetStockTestForm()
        lblSymbol.Text = String.Empty
        lblStockRate.Text = String.Empty
        lblStockChangeValue.Text = String.Empty
        lblStockChangePercent.Text = String.Empty
        lblDateOfTrade.Text = String.Empty
    End Sub
    'Validation Code
    Private Function ValidateStockTestForm() As Boolean
        If cmbExchange.Text = "" Or cmbExchange.SelectedIndex = -1 Then
            MsgBox("Please select Exchange", MsgBoxStyle.Information)
            cmbExchange.Focus()
            Return False
        ElseIf txtSymbol.Text = "" Then
            MsgBox("Symbol cannot be empty", MsgBoxStyle.Information)
            txtSymbol.Focus()
            Return False
        Else
            Return True
        End If
    End Function

    Private Sub btnGetQuotes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetQuotes.Click
        ResetStockTestForm()
        If ValidateStockTestForm() = True Then
            Dim IncomingData As StockQuoteData
            If cmbExchange.Text = "BSE" Then
                'Get Stock Rate with Exchange as BSE
                IncomingData = GetStockPriceBSE(txtSymbol.Text.Trim)
            Else
                ' Check the Stock Rate with Exchange as NSE
                IncomingData = GetStockPriceNSE(txtSymbol.Text.Trim)
            End If
            If IncomingData.StockChangeDownwards = True Then
                lblStockRate.ForeColor = Color.Red
                lblStockChangeValue.ForeColor = Color.Red
                lblStockChangePercent.ForeColor = Color.Red
                PictureBox1.Load("images\redarrow.gif")
            Else
                lblStockRate.ForeColor = Color.Green
                lblStockChangeValue.ForeColor = Color.Green
                lblStockChangePercent.ForeColor = Color.Green
                PictureBox1.Load("images\greenarrow.gif")
            End If
            lblSymbol.Text = IncomingData.Symbol
            lblStockRate.Text = IncomingData.StockRate
            lblStockChangeValue.Text = IncomingData.StockChangeValue
            lblStockChangePercent.Text = IncomingData.StockChangePercent
            lblDateOfTrade.Text = IncomingData.DateOfTrade
            PictureBox1.Visible = True
        End If
    End Sub
    ' Check the Stock Rate with Exchange as BSE
    Public Function GetStockPriceBSE(ByVal strSymbol As String) As StockQuoteData
        Dim StockRateProvider As New Yahoo.StockQuotes
        Dim IncomingData As StockQuoteData
        IncomingData = StockRateProvider.GetStockQuote(strSymbol, IndiaExchange.BSE)
        Return IncomingData
    End Function
    ' Check the Stock Rate with Exchange as NSE
    Public Function GetStockPriceNSE(ByVal stockSymbol As String) As StockQuoteData
        Dim target As IStockQuoteProvider = New NSE.StockQuotes
        Dim stockExchange As GlobalDefnsAndFunctions.IndiaExchange
        Dim actual As GlobalDefnsAndFunctions.StockQuoteData
        actual = target.GetStockQuote(stockSymbol, stockExchange)
        Return actual
    End Function

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub
End Class
